package com.skyline.wo.dao.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.skyline.base.type.RemoteDeviceType;
import com.skyline.common.bean.Page;
import com.skyline.common.cache.annotation.Cache;
import com.skyline.common.cache.annotation.CacheCategoryType;
import com.skyline.common.cache.annotation.CacheDelete;
import com.skyline.common.cache.annotation.Fk;
import com.skyline.common.cache.annotation.Param;
import com.skyline.common.mongo.MongoTemplate;
import com.skyline.wo.dao.ShortMessageDao;
import com.skyline.wo.mapper.ShortMessageMapper;
import com.skyline.wo.model.ShortMessage;

@Repository("shortMessageDao")
public class ShortMessageDaoImpl implements ShortMessageDao {

	// @Value("${ShortMessageDao.insertShortMessage}")
	// private String insertShortMessageSql;

	// @Value("${ShortMessageDao.queryShortMessage}")
	// private String queryShortMessageSql;
	//
	// @Value("${ShortMessageDao.queryShortMessageByOwnerId}")
	// private String queryShortMessageByOwnerIdSql;
	//
	// @Value("${ShortMessageDao.deleteShortMessage}")
	// private String deleteShortMessageSql;

	@Value("${shortMessage.collectionName}")
	private String collectionName;

	@Autowired
	private MongoTemplate mongoTemplate;

	private DBCollection shortMessageCollection = null;

	private void inital() {
		if (shortMessageCollection == null) {
			try {
				shortMessageCollection = mongoTemplate.getCollection(collectionName);
				if (shortMessageCollection == null) {
					throw new Exception();
					// TODO:对这里的异常进行处理
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	@CacheDelete(type = CacheCategoryType.SHORTMESSAGE)
	public String insertShortMessage(Long ownerId, String ownerNickname, String ownerPortrait, String text, Long sourceOwnerId,
			String sourceOwnerNickname, String comment, List<Long> commetterIds, List<String> commetterNicknames, List<String> comments,
			Double spotLat, Double spotLng, Long spotId, String spotName, RemoteDeviceType remoteDeviceType) {
		inital();
		Date createTime = new Date();
		BasicDBObject shortMessage = ShortMessageMapper.map2DBObject(ownerId, ownerNickname, ownerPortrait, text, sourceOwnerId,
				sourceOwnerNickname, comment, createTime, commetterIds, commetterNicknames, comments, spotLat, spotLng, spotId, spotName,
				remoteDeviceType);
		shortMessageCollection.insert(shortMessage);
		Object id = shortMessage.get("_id");
		if (id == null) {
			return null;
		} else {
			return id.toString();
		}
	}

	@Override
	@Cache(keyPattern = "shortMess-:id", type = CacheCategoryType.SHORTMESSAGE)
	public ShortMessage queryShortMessage(@Param("id") String id) {
		inital();
		BasicDBObject condition = ShortMessageMapper.getQueryConditionById(id);
		DBObject obj = shortMessageCollection.findOne(condition);
		if (obj == null) {
			return null;
		} else {
			return ShortMessageMapper.map2ShortMessage(obj);
		}
	}

	@Override
	@Cache(keyPattern = "shortMess-l-:uid", type = CacheCategoryType.SHORTMESSAGE)
	public List<ShortMessage> queryShortMessageByOwnerId(@Param("uid") Long ownerId, Page page) {
		inital();
		BasicDBObject condition = ShortMessageMapper.getQueryConditionByOwnerId(ownerId);
		page.setTotal((int) shortMessageCollection.count(condition));
		DBCursor cursor = shortMessageCollection.find(condition).skip(page.getStartIndex()).limit(page.getSize()).sort(ShortMessageMapper.getSortCondition());
		return ShortMessageMapper.map2ShortMessages(cursor);
	}

	@Override
	@CacheDelete(type = CacheCategoryType.SHORTMESSAGE)
	public void deleteShortMessage(long id, @Fk long actionerId) {
		inital();
		// jdbcTemplate.update(deleteShortMessageSql, id, actionerId);
	}
}
